package com.RecipeBook.shopping;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ResourceCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.RecipeBook.browse.DataBaseHelper;
import com.RecipeBook.common.*;
import com.RecipeBook.main.GlobalsManager;
import com.RecipeBook.main.R;

public class ShoppingList extends RecipeActivity {

	private class ShoppingAdapter extends ArrayAdapter<String> {

		private ArrayList<String> tempList;

		public ShoppingAdapter(Context context, int textViewResourceId,
				List<String> objects) {
			super(context, textViewResourceId, objects);
			//recipes = new ArrayList<RecipeInProgress>(objects);
			tempList = new ArrayList<String>(objects);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.shopping_list_item, null);
			}

			String temp = tempList.get(position);
			if (temp != null) {
				TextView name = (TextView) v.findViewById(R.id.TextView01);
				CheckBox check = (CheckBox)v.findViewById(R.id.list_checkbox);
				if (name != null && check != null) {
					name.setText(temp);
					check.setChecked(false);
					check.setFocusable(false);
				}

			}
			return v;
		}
	}


	AlertDialog alert = null;
	ArrayList<RecipeInProgress> ripl;
	ArrayList<Ingredient> ingredientList;
	ArrayList<String> ingredientNames = new ArrayList<String>();
	ArrayList<String> ingredientDescriptions = new ArrayList<String>();
	Recipe r;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shopping);

		DataBaseHelper dbh = GlobalsManager.getGlobals().getDatabaseHelper();
		dbh.openDataBase();

		ripl = GlobalsManager.getGlobals().getRecipesInProgress();
		
		for (RecipeInProgress rec: ripl){
			for (Ingredient ing: rec.getRecipe().getIngredients()){
				ingredientNames.add(ing.getName());
				ingredientDescriptions.add(ing.getDesc());
			}
		}
		
		final ListView lv=(ListView)findViewById(R.id.ListView01);
		
		if (ripl.isEmpty())
		{
			lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,new String[]{"No recipes in progress"}));
		}
		else
		{
			lv.setAdapter(new ShoppingAdapter(this, android.R.layout.simple_list_item_1, ingredientNames));

			final AlertDialog.Builder builder = new AlertDialog.Builder(this);
			lv.setOnItemClickListener(new OnItemClickListener()
			{

				public void onItemClick(AdapterView<?> arg0, final View arg1, int arg2,
						long arg3) 
				{
					final String ingred = lv.getItemAtPosition(arg2).toString();
					String description = getDescription(arg2);
					
					builder.setTitle(ingred +":");
					builder.setMessage(description);
					
					alert = builder.create();
					alert.show();
				}
			});	
		}
	} // end OnCreate method
	
	
	public String getDescription(int pos) {
		
		String description = ingredientDescriptions.get(pos);
		if (description == null)
		{
			return "No description for this item";
		}		
		return description;
	} // end getDescription method

}
